package com.wang.task;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.common.pojo.BasePojoEnum;
import com.wang.common.util.ConstatFinalUtil;
import com.wang.common.util.DateUtil;
import com.wang.users.pojo.AtCar;
import com.wang.users.pojo.AtCarQuery;
import com.wang.users.pojo.AtIntersection;
import com.wang.users.pojo.AtIntersectionQuery;
import com.wang.users.pojo.AtRoute;
import com.wang.users.pojo.AtRouteQuery;
import com.wang.users.pojo.AtStat;
import com.wang.users.service.AtCarService;
import com.wang.users.service.AtIntersectionService;
import com.wang.users.service.AtRouteService;
import com.wang.users.service.AtStatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * 一个模块一个定时任务类
 */
@Slf4j
@Component
public class UsersTask {
    @Autowired
    private AtCarService carService;
    @Autowired
    private AtIntersectionService intersectionService;
    @Autowired
    private AtRouteService routeService;
    @Autowired
    private AtStatService statService;
    @Autowired
    private DateUtil dateUtil;

    /**
     * 处理的逻辑: 模拟车辆随机经过的路口(每隔一分钟执行一次)
     * <pre>
     *      1.查询所有可用车辆,
     *      2.查询所有可用路口
     *      3.随机选中一条车辆和一条路口,往路线表中增加一条记录
     *  </pre>
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    @Transactional(rollbackFor = Exception.class)
    public void proccedRandRoute() {
        log.info("模拟线路开始");
        ConstatFinalUtil.randSleep(ConstatFinalUtil.FINAL_SLOW);
        /* 最大生成的总条数 */
        int totalRandomCount = 10;
        Random random = new Random();

        /* 查询所有的可用车辆 */
        AtCarQuery carQuery = new AtCarQuery();
        AtCar carEntity = carQuery.getEntity();
        carEntity.setDelFlag(BasePojoEnum.DELFLAG_NO.getCode());
        carEntity.setStatus(BasePojoEnum.STATUS_ENABLE.getCode());
        List<AtCar> carList = this.carService.findList(null, carQuery);
        /* 查询所有交通灯数据 */
        AtIntersectionQuery interQuery = new AtIntersectionQuery();
        AtIntersection interEntity = interQuery.getEntity();
        interEntity.setDelFlag(BasePojoEnum.DELFLAG_NO.getCode());
        interEntity.setStatus(BasePojoEnum.STATUS_ENABLE.getCode());
        List<AtIntersection> intersectionList = this.intersectionService.findList(null, interQuery);
        int totalCount = random.nextInt(totalRandomCount);
        log.info("模拟生成的线路数据条数,生成条数:{}", totalCount);
        for (int i = 0; i < totalCount; i++) {
            int carRandomIndex = random.nextInt(carList.size());
            int interRandomIndex = random.nextInt(intersectionList.size());
            AtCar car = carList.get(carRandomIndex);
            AtIntersection intersection = intersectionList.get(interRandomIndex);
            /* 生成线路 */
            AtRoute route = new AtRoute();
            route.setCarId(car.getId());
            route.setInterId(intersection.getId());
            boolean dbFlag = this.routeService.save(route);
            log.info("生成一条线路数据,当前条数:{},车辆:{},路口:{},结果:{}", i, route.getCarId(), route.getInterId(), dbFlag);
        }
        log.info("模拟线路结束,生成条数:{}", totalCount);
    }

    /**
     * 实时计算路口的统计数据
     * <pre>
     *     每隔2分钟查询数据.
     * </pre>
     */
    @Scheduled(cron = "0 0/2 * * * ?")
    @Transactional(rollbackFor = Exception.class)
    public void proccedRandStat() {
        log.info("模拟统计开始");
        Calendar calNow = Calendar.getInstance();
        calNow.set(Calendar.SECOND, 0);
        calNow.set(Calendar.MILLISECOND, 0);

        Date currTime = calNow.getTime();
        calNow.add(Calendar.MINUTE, -2);
        Date stTime = calNow.getTime();

        /* 查询所有交通灯数据 */
        AtIntersectionQuery interQuery = new AtIntersectionQuery();
        AtIntersection interEntity = interQuery.getEntity();
        interEntity.setDelFlag(BasePojoEnum.DELFLAG_NO.getCode());
        interEntity.setStatus(BasePojoEnum.STATUS_ENABLE.getCode());
        List<AtIntersection> intersectionList = this.intersectionService.findList(null, interQuery);
        for (AtIntersection dataTemp : intersectionList) {
            /* 查询过去两分钟的数据 */
            IPage<AtRoute> routePage = new Page<>(1, 10);
            AtRouteQuery routeQuery = new AtRouteQuery();
            AtRoute routeEntity = routeQuery.getEntity();
            routeEntity.setInterId(dataTemp.getId());
            routeEntity.setDelFlag(BasePojoEnum.DELFLAG_NO.getCode());
            routeEntity.setStatus(BasePojoEnum.STATUS_ENABLE.getCode());
            routeQuery.setCreateTimeSt(stTime);
            routeQuery.setCreateTimeEd(currTime);
            this.routeService.findList(routePage, routeQuery);
            /* 存储统计数据 */
            AtStat stat = new AtStat();
            stat.setInterId(dataTemp.getId());
            stat.setCarNum(Integer.parseInt(routePage.getTotal() + ""));
            stat.setCurrTime(calNow.getTime());
            boolean dbFlag = this.statService.save(stat);
            log.info("生成一条统计数据,路口:{},结果:{}", stat.getInterId(), dbFlag);
        }
        log.info("模拟统计结束");
    }
}
